﻿using MediatR;
using WarehouseManagement.Application.Command.ShipApplyCommand;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.ShipApplyCommandHandlers
{
    /// <summary>
    /// 发货确认列表
    /// </summary>
    public class GetDeliveryConfirmationCommandHandler:IRequestHandler<GetDeliveryConfirmationCommand, PageDeliveryConfirmation>
    {
        private readonly lnterfaceDeliveryConfirmationRepository _deliveryConfirmationRepository;

        public GetDeliveryConfirmationCommandHandler(lnterfaceDeliveryConfirmationRepository deliveryConfirmationRepository)
        {
            _deliveryConfirmationRepository = deliveryConfirmationRepository;
        }

        public async Task<PageDeliveryConfirmation> Handle(GetDeliveryConfirmationCommand request, CancellationToken cancellationToken)
        {
            var res = (await _deliveryConfirmationRepository.GetAll()).Where(x => x.IsDel == false);
            if (!string.IsNullOrEmpty(request.SPRTopic))
            {
                res = res.Where(x => x.SPRTopic.Contains(request.SPRTopic));
            }
            if (!string.IsNullOrEmpty(request.SRADueDate))
            {
                res = res.Where(x => x.SRADueDate >= Convert.ToDateTime(request.SRADueDate));
            }
            if (!string.IsNullOrEmpty(request.SSRADueDate))
            {
                res = res.Where(x => x.SRADueDate <= Convert.ToDateTime(request.SSRADueDate));
            }
            if (!string.IsNullOrEmpty(request.SRAApplicant))
            {
                res = res.Where(x => x.SRAApplicant ==request.SRAApplicant);
            }
            if (!string.IsNullOrEmpty(request.DeliveryConfirmationNumber))
            {
                res = res.Where(x => x.DeliveryConfirmationNumber == request.DeliveryConfirmationNumber);
            }
            if (!string.IsNullOrEmpty(request.VName))
            {
                res = res.Where(x => x.VName == request.VName);
            }
            PageDeliveryConfirmation page = new PageDeliveryConfirmation();
            page.SumCount = res.Count();
            page.PageLast = (int)Math.Ceiling(page.SumCount * 1.0 / request.PageSize);
            page.list = res.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;

        }
    }
}
